<?php
/**
 * @title 336. 回文对 - 超时硬遍历
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param String[] $words
     * @return Integer[][]
     */
    function palindromePairs($words) {
        /**
         * @since 2020-08-06 结果集
         */
        $result = [];

        /**
         * @since 2020-08-06 遍历
         */
        for($i=0, $len=count($words); $i<$len; $i++){
            $wordA = $words[$i];

            for($j=$i+1; $j<$len; $j++){
                $wordB = $words[$j];

                if(self::find($wordA, $wordB)){
                    $result[] = [$i, $j];
                } else {}
                if(self::find($wordB, $wordA)){
                    $result[] = [$j, $i];
                } else {}
            }
        }

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 比较2个字符拼接是否回文
     * @author start2004
     * @since 2020-08-06 Created
     *
     * @param string $wordA
     * @param string $wordB
     * @return boolean
     */
    public function find($wordA = "", $wordB = ""){
        $str = $wordA . $wordB;

        /**
         * @since 2020-08-06 左右指针
         */
        $left = 0;
        $right = strlen($str)-1;
        while($left<$right){
            if($str[$left] !== $str[$right]){
                return false;
                break;
            } else {}
            $left++;
            $right--;
        }

        /**
         * @return
         */
        return true;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.06.php
 */
$datas = [
    ["abcd","dcba","lls","s","sssll"],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->palindromePairs($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
